/**
 * @fileOverview 
 * Contains JS code used by the map demo.
 * @author Igor Kulkin <igor.kulkin@gmail.com>
 */


var metersToMercator = geoPolygonUtils.googleMapsAdapter.metersToMercator;


var polygonFromGPolygon = geoPolygonUtils.googleMapsAdapter.polygonFromGPolygon;


var polygonToGPolygon = geoPolygonUtils.googleMapsAdapter.polygonToGPolygon;
 

function MapDemoApp(container) {
  this.map_ = new GMap2(document.getElementById("map_canvas"));
  this.map_.setCenter(new GLatLng(55.755738, 37.616801), 15);
  this.map_.setUIToDefault();
  
  this.gPolygon_ = new GPolygon([]);
  this.map_.addOverlay(this.gPolygon_);
  
  this.events = [];
  this.enterEditMode(); 
}


MapDemoApp.prototype.removeEvents_ = function() {
  for (var i = 0; i < this.events.length; ++i) {
    GEvent.removeListener(this.events[i]);
  }
  this.events = [];
};


MapDemoApp.prototype.updatePolygon = function(newGPolygon) {
  this.map_.removeOverlay(this.gPolygon_);
  this.gPolygon_ = newGPolygon;
  this.map_.addOverlay(this.gPolygon_);
};


MapDemoApp.prototype.enterEditMode = function() {
  this.disableControls();
  
  var editButton = document.getElementById('edit');
  editButton.onclick = GEvent.callback(this, this.exitEditMode);
  
  if (this.gPolygon_.getVertexCount() == 0) {
    this.gPolygon_.enableDrawing();
    editButton.style.visibility = 'hidden';
    this.events.push(GEvent.bind(
        this.gPolygon_, 'endline', this, this.exitEditMode));
    this.events.push(GEvent.bind(
        this.gPolygon_, 'cancelline', this, this.exitEditMode));
  } else {
    this.gPolygon_.enableEditing();
    editButton.style.visibility = 'visible';
    editButton.innerHTML = 'Done';
  }
};


MapDemoApp.prototype.exitEditMode = function() {
  this.removeEvents_();
  
  this.gPolygon_.disableEditing();
  
  var editButton = document.getElementById('edit');
  editButton.style.visibility = 'visible';
  editButton.innerHTML = 'Edit';
  editButton.onclick = GEvent.callback(this, this.enterEditMode);
  
  this.enableControls();
};


MapDemoApp.prototype.disableControls = function() {
  var controlsElement = document.getElementById('controls');
  controlsElement.style.visibility = 'hidden';
};


MapDemoApp.prototype.enableControls = function() {
  var controlsElement = document.getElementById('controls');
  controlsElement.style.visibility = 'visible';
  
  var me = this;
  document.getElementById('simplify').onclick = function() {
    var polygon = polygonFromGPolygon(me.gPolygon_);
    var newPolygon = geoPolygonUtils.simplifyPolygon(polygon, metersToMercator(10));
    if (newPolygon) {
      me.updatePolygon(polygonToGPolygon(newPolygon));
    }
  };
  document.getElementById('correct').onclick = function() {
    var polygon = polygonFromGPolygon(me.gPolygon_);
    var newPolygon = geoPolygonUtils.correctAngles(
        polygon, Math.PI * 0.5, Math.PI * 0.05);
    if (newPolygon) {
      me.updatePolygon(polygonToGPolygon(newPolygon));
    }
  };
};



function LatlngMercatorConverter(latlngInput, mercatorInput) {
  this.projection_ = new GMercatorProjection(20);
  
  var me = this;
  
  
  latlngInput.onclick = function() {
    if (this.value == 'n/a') {
      this.value = '';
    }
  };
  
  
  mercatorInput.onfocus = function() {
    if (this.value == 'n/a') {
      this.value = '';
    }
  };
  
  
  latlngInput.onchange = function() {
    var latlng = me.parseNumbersPair(this.value);
    if (latlng) {
      mercatorInput.value = me.formatNumbersPair(me.latlngToMercator(latlng));
    } else {
      mercatorInput.value = 'n/a';
    }
  };
  
  
  mercatorInput.onchange = function() {
    var mercator = me.parseNumbersPair(this.value);
    if (mercator) {
      latlngInput.value = me.formatNumbersPair(me.latlngFromMercator(mercator));
    } else {
      latlngInput.value = 'n/a';
    }
  };
}


LatlngMercatorConverter.prototype.parseNumbersPair = function(s) {
  var tokens = s.split(',');
  if (tokens.length != 2) {
    return null;
  }
  var x = Number(tokens[0]);
  var y = Number(tokens[1]);
  if (typeof(x) == 'number' && typeof(y) == 'number') {
    return [x, y];
  } else {
    return null;
  }
};


LatlngMercatorConverter.prototype.formatNumbersPair = function(n) {
  return n[0].toFixed(8) + ', ' + n[1].toFixed(8);
};


LatlngMercatorConverter.prototype.latlngToMercator = function(latlng) {
  var pixel = this.projection_.fromLatLngToPixel(new GLatLng(latlng[0], latlng[1]), 19);
  var x = pixel.x * 1.0 / (1 << 27);
  var y = pixel.y * 1.0 / (1 << 27);
  return [x, y];
};


LatlngMercatorConverter.prototype.latlngFromMercator = function(mercator) {
  var x = mercator[0] * (1 << 27);
  var y = mercator[1] * (1 << 27);
  var latlng = this.projection_.fromPixelToLatLng(new GPoint(x, y), 19, true);
  return [latlng.lat(), latlng.lng()];
};


var mapApp = null;


var converter = null;

  
function initialize() {
  if (GBrowserIsCompatible()) {
    mapApp = new MapDemoApp(document.getElementById("map_canvas"));
    converter = new LatlngMercatorConverter(
        document.getElementById("latlng"),
        document.getElementById("mercator"));
  } else {
    document.getElementById('warning').style.display = 'block';
  }
}